home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / sox_2 / sox / c / sndrtool < prev    next >
Text File  |  1993-09-23  |  4KB  |  165 lines

  1. /*
  2.  * Sounder/Sndtool format handler: W V Neisius, February 1992
  3.  *
  4.  * June 28, 93: force output to mono.
  5.  */
  6.  
  7. #include <math.h>
  8. #include "st.h"
  9. #ifdef    VMS
  10. #include <errno.h>
  11. #include <perror.h>
  12. #endif
  13.  
  14. /* Private data used by writer */
  15. struct sndpriv {
  16.         unsigned long nsamples;
  17. };
  18.  
  19. #ifndef    SEEK_CUR
  20. #define    SEEK_CUR    1
  21. #endif
  22.  
  23. IMPORT sndtwriteheader(P2(ft_t ft,long nsamples));
  24.  
  25. /*======================================================================*/
  26. /*                         SNDSTARTREAD                                */
  27. /*======================================================================*/
  28.  
  29. sndtstartread(ft)
  30. ft_t ft;
  31. {
  32. struct sndpriv *p = (struct sndpriv *) ft->priv;
  33.  
  34. char buf[97];
  35.  
  36. long rate;
  37.  
  38. rate = 0;
  39.  
  40. /* determine file type */
  41.         /* if first 5 bytes == SOUND then this is probably a sndtool sound */
  42.         /* if first word (16 bits) == 0 
  43.          and second word is between 4000 & 25000 then this is sounder sound */
  44.         /* otherwise, its probably raw, not handled here */
  45.  
  46. if (fread(buf, 1, 2, ft->fp) != 2)
  47.     fail("SND: unexpected EOF");
  48. if (strncmp(buf,"\0\0",2) == 0)
  49.     {
  50.     /* sounder */
  51.     rate = rlshort(ft);
  52.     if (rate < 4000 || rate > 25000 )
  53.         fail ("SND: sample rate out of range");
  54.     fseek(ft->fp,4,SEEK_CUR);
  55.     }
  56. else
  57.     {
  58.     /* sndtool ? */
  59.     fread(&buf[2],1,6,ft->fp);
  60.     if (strncmp(buf,"SOUND",5))
  61.         fail ("SND: unrecognized SND format");
  62.     fseek(ft->fp,12,SEEK_CUR);
  63.     rate = rlshort(ft);
  64.     fseek(ft->fp,6,SEEK_CUR);
  65.     if (fread(buf,1,96,ft->fp) != 96)
  66.         fail ("SND: unexpected EOF in SND header");
  67.     report ("%s",buf);
  68.     }
  69.  
  70. ft->info.channels = 1;
  71. ft->info.rate = rate;
  72. ft->info.style = UNSIGNED;
  73. ft->info.size = BYTE;
  74.  
  75. }
  76.  
  77. /*======================================================================*/
  78. /*                         SNDTSTARTWRITE                               */
  79. /*======================================================================*/
  80. sndtstartwrite(ft)
  81. ft_t ft;
  82. {
  83. struct sndpriv *p = (struct sndpriv *) ft->priv;
  84.  
  85. /* write header */
  86. ft->info.channels = 1;
  87. ft->info.style = UNSIGNED;
  88. ft->info.size = BYTE;
  89. p->nsamples = 0;
  90. sndtwriteheader(ft, 0);
  91.  
  92. }
  93. /*======================================================================*/
  94. /*                         SNDRSTARTWRITE                               */
  95. /*======================================================================*/
  96. sndrstartwrite(ft)
  97. ft_t ft;
  98. {
  99. /* write header */
  100. ft->info.channels = 1;
  101. ft->info.style = UNSIGNED;
  102. ft->info.size = BYTE;
  103.  
  104. /* sounder header */
  105. wlshort (ft,0); /* sample size code */
  106. wlshort (ft,(int) ft->info.rate);     /* sample rate */
  107. wlshort (ft,10);        /* volume */
  108. wlshort (ft,4); /* shift */
  109. }
  110.  
  111. /*======================================================================*/
  112. /*                         SNDTWRITE                                     */
  113. /*======================================================================*/
  114.  
  115. sndtwrite(ft, buf, len)
  116. ft_t ft;
  117. long *buf, len;
  118. {
  119.     struct sndpriv *p = (struct sndpriv *) ft->priv;
  120.     p->nsamples += len;
  121.     rawwrite(ft, buf, len);
  122. }
  123.  
  124. /*======================================================================*/
  125. /*                         SNDTSTOPWRITE                                */
  126. /*======================================================================*/
  127.  
  128. sndtstopwrite(ft)
  129. ft_t ft;
  130. {
  131. struct sndpriv *p = (struct sndpriv *) ft->priv;
  132.  
  133. /* fixup file sizes in header */
  134. if (fseek(ft->fp, 0L, 0) != 0)
  135.     fail("can't rewind output file to rewrite SND header");
  136. sndtwriteheader(ft, p->nsamples);
  137. }
  138.  
  139. /*======================================================================*/
  140. /*                         SNDTWRITEHEADER                              */
  141. /*======================================================================*/
  142. sndtwriteheader(ft,nsamples)
  143. ft_t ft;
  144. long nsamples;
  145. {
  146. char name_buf[97];
  147.  
  148. /* sndtool header */
  149. fputs ("SOUND",ft->fp); /* magic */
  150. fputc (0x1a,ft->fp);
  151. wlshort (ft,(long)0);  /* hGSound */
  152. wllong (ft,nsamples);
  153. wllong (ft,(long)0);
  154. wllong (ft,nsamples);
  155. wlshort (ft,(int) ft->info.rate);
  156. wlshort (ft,0);
  157. wlshort (ft,10);
  158. wlshort (ft,4);
  159. sprintf (name_buf,"%s - File created by Sound Exchange",ft->filename);
  160. fwrite (name_buf, 1, 96, ft->fp);
  161.  
  162. }
  163.  
  164.  
  165.